package 力扣日常刷题.木22二月.第20天226;

/**
 * @author 帅小伙
 * @date 2022/2/26
 * @description
 * 按住一个  检测高位
 * https://leetcode-cn.com/problems/1nzheng-shu-zhong-1chu-xian-de-ci-shu-lcof/solution/mian-shi-ti-43-1n-zheng-shu-zhong-1-chu-xian-de-2/
 */
public class Demo159整数中1的个数 {

        public int countDigitOne(int n) {
            int digit = 1,res = 0;
            // high  当前检测数字的高位   cur  当前的数字  low 低位
            int high = n / 10,cur = n % 10,low = 0;

            while (high != 0 || cur != 0) {
                // 此位1的数量只由高位决定
                if(cur == 0) res += high *digit;
                //
                else if(cur == 1) res += high * digit + low + 1;
                else res += (high + 1) * digit;
                low += cur * digit;
                cur = high % 10;
                high /= 10;
                digit *= 10;
            }
            return res;
        }

}
